<?php
namespace App\Wen\Utils;

use App\Http\Controllers\Api\Repositories\WxShopGoodsRepositories;
use App\Lib\Taoke\CheckSign;
use App\Models\Shop\WxShopClassify;
use App\Models\Shop\WxShopGoods;
use Illuminate\Support\Facades\Cache;
use Yansongda\Supports\Log;

class TaokeUtils{

    private static $appKey = '';
    private static $appSecret = '';
    private static $jd_unionid = '';
    public static $classify_id = 0;

    private static function get_app_key(){
        if(_empty_(self::$appKey)){
            self::$appKey = Settings::get('app_dataoke_appkey');
        }
        return self::$appKey;
    }

    private static function get_app_secret(){
        if(_empty_(self::$appSecret)){
            self::$appSecret = Settings::get('app_dataoke_appsecret');
        }
        return self::$appSecret;
    }
    private static function get_jd_unionid(){
        if(_empty_(self::$jd_unionid)){
            self::$jd_unionid = Settings::get('app_dataoke_jd_unionId');
        }
        return self::$jd_unionid;
    }

    /**
     * @param $dtk_id
     * @param $tk_goodsid 确保淘宝的goodsid是正确的
     * @param $tk_platform 0: 未知 1: 淘宝 2：天猫 3: 京东 4：拼多多 5:抖音
     * @return int[]
     */
    public static function get_goods_id_from_taoke($dtk_id, $tk_goodsid, $tk_platform, $title = ''){

        $result = ['code'=>0, 'data'=>0];
        $id = 0;

        if($dtk_id > 0){
            $id = WxShopGoods::where('dtk_id', $dtk_id)->value('id');
        }

        if(_empty_($id)){

            if($dtk_id > 0 && _empty_($tk_goodsid)){
                // 仅仅有dtk_id的情况下
                $insert_id = self::insert_dtk_id_goods($dtk_id);
                if($insert_id > 0){
                    $result['code'] = 1;
                    $result['data'] = $insert_id;
                    return $result;
                }
                $result['data'] = 200001;
                return $result;
            }

            $platform_id = '';
            if($tk_platform == 1 || $tk_platform == 2){

                if(strpos($tk_goodsid, '-') === false){
                    $platform_id = 'tb__' . $tk_goodsid;
                }else{
                    $tk_goodsid_arr = explode('-', $tk_goodsid);
                    update_good_meta(-1, 'tk:'.$tk_goodsid_arr[1], $tk_goodsid_arr[0]);
                    $platform_id = 'tb__' . $tk_goodsid_arr[1];
                }

            }else if($tk_platform == 3){
                $platform_id = 'jd__' . $tk_goodsid;
            }else if($tk_platform == 4){
                $platform_id = 'pdd__' . $tk_goodsid;
            }else if($tk_platform == 5){
                $platform_id = 'dy__' . $tk_goodsid;
            }



            if(_empty_($platform_id)){
                $result['data'] = 200001;
                return $result;
            }

            $id = WxShopGoods::where('tk_platform_id', $platform_id)->value('id');

            if(_empty_($id)){

                if($platform_id){

                    $insert_id = self::insert_platform_goods($platform_id);
                    if($insert_id > 0){
                        $result['code'] = 1;
                        $result['data'] = $insert_id;
                        return $result;
                    }

                }

                if($title){
                    $insert_id = self::insert_platform_goods($platform_id, $title);
                    if($insert_id > 0){
                        $result['code'] = 1;
                        $result['data'] = $insert_id;
                        return $result;
                    }
                }

                $result['data'] = 200001;
                return $result;
            }

        }

        $result['code'] = 1;
        $result['data'] = $id;
        return $result;
    }

    private static function insert_dtk_id_goods($dtk_id, $platform = 'tb'){
        if(_empty_($dtk_id)){
            return 0;
        }

        if($platform == 'tb'){
            $detail_res = self::get_taobao_item_detail($dtk_id, true);
            if(_empty_($detail_res) || $detail_res['code'] == 0){
                return 0;
            }else{
                $other_good_detail = $detail_res['data'];
                $link_res = self::get_taobao_item_link($dtk_id, true);
                if(_empty_($link_res) || $link_res['code'] == 0){
                    return 0;
                }else{
                    $other_good_link = $link_res['data'];
                    return self::create_mini_goods($other_good_detail, $other_good_link);
                }
            }
        }
    }



    private static function insert_platform_goods($platform_id, $title = ''){

        if(_empty_($platform_id)){
            return 0;
        }

        $platform_id_arr = explode('__', $platform_id);

        if($platform_id_arr[0] == 'tb'){
            if($platform_id_arr[1] == 0 && $title){
                $detail_res = self::get_taobao_item_detail_from_title($title);
            }else{
                $detail_res = self::get_taobao_item_detail($platform_id_arr[1]);
            }

            if(_empty_($detail_res) || $detail_res['code'] == 0){
                return 0;
            }else{
                $other_good_detail = $detail_res['data'];
                $link_res = self::get_taobao_item_link($platform_id_arr[1]);
                if(_empty_($link_res) || $link_res['code'] == 0){
                    return 0;
                }else{
                    $other_good_link = $link_res['data'];
                    return self::create_mini_goods($other_good_detail, $other_good_link);
                }
            }
        }else if($platform_id_arr[0] == 'jd'){
            $detail_res = self::get_jingdong_item_detail($platform_id_arr[1]);
            if(_empty_($detail_res) || $detail_res['code'] == 0){
                return 0;
            }else{
                $other_good_detail = $detail_res['data'];
                $link_res = self::get_jingdong_item_link($platform_id_arr[1]);
                if(_empty_($link_res) || $link_res['code'] == 0){
                    return 0;
                }else{
                    $other_good_link = $link_res['data'];
                    return self::create_mini_goods($other_good_detail, $other_good_link);
                }
            }
        }else if($platform_id_arr[0] == 'pdd'){
            $detail_res = self::get_pingduoduo_item_detail($platform_id_arr[1]);
            if(_empty_($detail_res) || $detail_res['code'] == 0){
                return 0;
            }else{
                $other_good_detail = $detail_res['data'];
                $link_res = self::get_pingduoduo_item_link($other_good_detail['pdd_goodsSign']);
                if(_empty_($link_res) || $link_res['code'] == 0){
                    return 0;
                }else{
                    $other_good_link = $link_res['data'];
                    return self::create_mini_goods($other_good_detail, $other_good_link);
                }
            }
        }else if($platform_id_arr[0] == 'dy'){
            $detail_res = self::get_douyin_item_detail($platform_id_arr[1]);
            if(_empty_($detail_res) || $detail_res['code'] == 0){
                return 0;
            }else{
                $other_good_detail = $detail_res['data'];
                $link_res = self::get_douyin_item_link($platform_id_arr[1], $other_good_detail['url']);
                if(_empty_($link_res) || $link_res['code'] == 0){
                    return 0;
                }else{
                    $other_good_link = $link_res['data'];
                    return self::create_mini_goods($other_good_detail, $other_good_link);
                }
            }
        }
        return 0;
    }

    private static function create_mini_goods($other_good_detail, $other_good_link){


        if(_empty_($other_good_detail) || _empty_($other_good_link)){
            return 0;
        }

        $content_other_good = '';
        if($other_good_detail['detailPics']){
            foreach ($other_good_detail['detailPics'] as $img){
                $content_other_good .= '<img loading="lazy" src="'.$img.'" >';
            }
        }

        $pics = [];
        if(!_empty_($other_good_detail['sliders'])){
            $pics = $other_good_detail['sliders'];
        }else{
            if($other_good_detail['main_img']){
                $pics[] = $other_good_detail['main_img'];
            }
        }


        $link = $other_good_link['link'];
        if($other_good_detail['platform'] == '淘宝' || $other_good_detail['platform'] == '天猫'){
            $link = $other_good_link['shortLink'];
        }

       $other_goods_id = explode('__', $other_good_detail['id'])[1];
        if($other_goods_id == 0 || strlen($other_goods_id) <= 4){
            return 0;
        }

        $force_classfy_id = 0;
        if(strpos($other_good_detail['title'], '药') && WxShopClassify::where([['id', '=', 6],['name', '=', '药品']])->exists()){
            $force_classfy_id = 6;
        }

        // $dtk_id, $tk_platform_id, $name, $pic, $tag, $intro, $vip_price, $price, $content, $buys, $sku, $url, $classify_id, $service_id
        $goods_id = WxShopGoodsRepositories::taoke_add($other_good_detail['dtk_id'], $other_good_detail['id'], $other_good_detail['title'],
            implode(',', $pics), [$other_good_detail['platform']], '', $other_good_detail['price'], $other_good_detail['price'],
            $content_other_good, $other_good_detail['monthSales'], '', $link, ($force_classfy_id > 0 ? $force_classfy_id : self::$classify_id), [5], $other_good_detail['word'] ?? ''
        );

        if($goods_id > 0){
            if($other_good_detail['platform'] == '拼多多' && $other_good_detail['pdd_goodsSign'] ){
                update_good_meta($goods_id, 'pdd_goodsSign', $other_good_detail['pdd_goodsSign'], 's');
            }
        }
        return $goods_id;
    }


    private static function init_other_good_item($others_goods_id, $platform = '', $main_img = '', $title = '', $price = '', $data = []){
        return ['id'=>$others_goods_id,'dtk_id' => -1, 'platform'=>$platform,'main_img'=>$main_img,'title'=>$title, 'price'=>$price, 'origin_price'=> 0,
            'parent_cat'=> '', 'child_cat'=> '',
            'brand' => ['brandId'=>'', 'brandName'=>''], 'seller'=>['sellerId'=>'', 'shopName'=>'', 'shopLevel'=>0, 'shopLogo'=>''],
            'sliders' => [], 'subtitle' => '', 'video'=> '', 'flag'=> '', 'monthSales'=> 0, 'detailPics'=> [], 'pdd_goodsSign'=> '', 'url' => '',
            'update_time'=>current_time('mysql'), 'data'=>$data];
    }

    private static function is_five_param_good($other_good){
        if($other_good['id'] && $other_good['platform'] && $other_good['main_img'] && $other_good['title'] && $other_good['price']){
            return true;
        }
        return false;
    }

    private static function init_other_good_link($others_goods_id, $link = '', $data = []){
        return ['id'=>$others_goods_id.'__link', 'platform_id'=>$others_goods_id, 'link'=>$link, 'coupon_link'=>'', 'word'=>'',
            'longWord'=>'', 'shortLink'=>'', 'data'=>$data];
    }

    public static function get_taobao_item_from_taokouling($taokouling){

        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/tb-service/parse-taokouling';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['content'] = $taokouling;
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__, $e->getMessage());
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0 && $result_arr['data']['goodsId']){
            return ['code'=>1, 'data'=>$result_arr['data']['goodsId']];
        }else{
            _logger_(__file__, __line__,  '*********************get_taobao_item_from_taokouling错误【淘口令:'.$taokouling.'】：'.$result_arr['msg'].'************************************');
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }

    }

    public static function get_taobao_item_from_content($taokouling){
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/dels/kit/contentParse';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['content'] = $taokouling;
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__, $e->getMessage());
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }
        if($result_arr['data'] && !_empty_($result_arr['data']['kuaiZhanUrl']) && $result_arr['data']['platType'] == 'taobao' && $result_arr['data']['itemId']){
            return ['code'=>1, 'data'=>$result_arr['data']['itemId']];
        }else{
            _logger_(__file__, __line__,  '*********************get_taobao_item_from_taokouling错误【淘口令:'.$taokouling.'】：'.$result_arr['msg'].'************************************');
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

    //淘宝商品信息
    private static function get_taobao_item_detail($goodsId, $is_dtk_id = false)
    {
        if(!$goodsId){
            return null;
        }

        $ask_key = 'goodsId';
        if($is_dtk_id){
            $ask_key = 'id';
        }

        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/goods/get-goods-details';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.2.3';
        //其他请求参数 根据接口文档需求选填
        $params = array();

        if($is_dtk_id){
            $params[$ask_key] = $goodsId;
        }else{
            if(strpos($goodsId, '-') === false){
    //            $pre = Cache::get('tk:'.$goodsId);
                $pre = get_good_meta(-1, 'tk:'.$goodsId);
                if($pre){
                    $params[$ask_key] = $pre . '-' . $goodsId;
                }else{
                    $params[$ask_key] = $goodsId;
                }
            }else{
                $params[$ask_key] = $goodsId;
                $goodsId = explode('-', $goodsId)[1];
            }
        }

        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__, $e->getMessage());
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            if($result_arr['data']['goodsSign']){
                if(strpos($result_arr['data']['goodsSign'], '-') === false){
                    _logger_(__file__, __line__, '大淘客返回的goodsSign不是新的，这个没有办法解决goodsSign：'.$result_arr['data']['goodsSign']);
                    return ['code'=>0, 'msg'=>'大淘客返回的goodsSign不是新的，这个没有办法解决'];
                }
                $goodsSign_arr = explode('-', $result_arr['data']['goodsSign']);

//                Cache::forget('tk:'.$goodsSign_arr[1]);
//                Cache::put('tk:'.$goodsSign_arr[1], $goodsSign_arr[0]);

                update_good_meta(-1, 'tk:'.$goodsSign_arr[1], $goodsSign_arr[0]);

                $goodsId = $goodsSign_arr[1];
            }

            // 基本内容
            $other_good = self::init_other_good_item('tb__'.$goodsId, $result_arr['data']['shopType'] == 1 ? '天猫' : '淘宝',
                $result_arr['data']['mainPic'], $result_arr['data']['title'],
                $result_arr['data']['actualPrice'],$result_arr['data']);


            if($result_arr['data']['id'] > 0){
                $other_good['dtk_id'] = $result_arr['data']['id'];
            }

            if(isset($result_arr['data']['brandId']) && $result_arr['data']['brandId']){
                $other_good['brand']['brandId'] = _array_key($result_arr['data'], 'brandId', '');
                $other_good['brand']['brandName'] = _array_key($result_arr['data'], 'brandName', '');
            }
            if(isset($result_arr['data']['sellerId']) && $result_arr['data']['sellerId']){
                $other_good['seller']['sellerId'] = _array_key($result_arr['data'], 'sellerId', '');
                $other_good['seller']['shopName'] = _array_key($result_arr['data'], 'shopName', '');
                $other_good['seller']['shopLevel'] = _array_key($result_arr['data'], 'shopLevel', '');
                $other_good['seller']['shopLogo'] = _array_key($result_arr['data'], 'shopLogo', '');
            }

            if(isset($result_arr['data']['originalPrice']) && $result_arr['data']['originalPrice']){
                $other_good['origin_price'] = $result_arr['data']['originalPrice'];;
            }
            if(isset($result_arr['data']['imgs']) && $result_arr['data']['imgs']){
                $other_good['sliders'] = explode(',', $result_arr['data']['imgs']);
            }
//                if($result_arr['data']['specialText']){
//                    $other_good['flag'] = $result_arr['data']['specialText'] ? $result_arr['data']['specialText'][0] : '';
//                }
            if(isset($result_arr['data']['smallImages']) && $result_arr['data']['smallImages'] && !is_array($result_arr['data']['smallImages'])){
                $result_arr['data']['smallImages'] = json_decode($result_arr['data']['smallImages'], true);
            }
            if(isset($result_arr['data']['smallImages']) && $result_arr['data']['smallImages'] && is_array($result_arr['data']['smallImages'])){
                if(_empty_($other_good['sliders'])){
                    $other_good['sliders'] = $result_arr['data']['smallImages'];
                }else{
                    foreach ($result_arr['data']['smallImages'] as $img){
                        $other_good['sliders'][] = $img;
                    }
                }
            }
            $other_good['flag'] = $result_arr['data']['shopType'] == 1 ? '天猫' : '淘宝';
            if(isset($result_arr['data']['desc']) && $result_arr['data']['desc']){
                $other_good['subtitle'] = $result_arr['data']['desc'];
            }
            if(!$other_good['subtitle'] && isset($result_arr['data']['dtitle']) && $result_arr['data']['dtitle']){
                $other_good['subtitle'] = $result_arr['data']['dtitle'];;
            }
            if(isset($result_arr['data']['video']) && $result_arr['data']['video']){
                $other_good['video'] = $result_arr['data']['video'];
            }
            if(isset($result_arr['data']['monthSales']) && $result_arr['data']['monthSales']){
                $other_good['monthSales'] = $result_arr['data']['monthSales'];
            }
            if(isset($result_arr['data']['detailPics']) && $result_arr['data']['detailPics']){
                $detailPics_arr = json_decode($result_arr['data']['detailPics'], true);
                if($detailPics_arr && count($detailPics_arr) > 1){
                    foreach ($detailPics_arr as $detailPics){
                        $other_good['detailPics'][] = $detailPics['img'];
                    }
                }else{
                    preg_match_all('$"img": "(.*?)"$', $result_arr['data']['detailPics'], $match_imgs);
                    if($match_imgs[1]){
                        foreach ($match_imgs[1] as $img_src){
                            $other_good['detailPics'][] = $img_src;
                        }
                    }
                }
            }

            if(_empty_($other_good['detailPics'])){
                $other_good['detailPics'] = $other_good['sliders'];
            }
            return ['code'=>1, 'data'=>$other_good];
        }else{
            _logger_(__file__, __line__,  '*********************get_taobao_item_detail错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************');
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }


    //淘宝商品信息
    private static function get_taobao_item_detail_from_title($title)
    {
        if(!$title){
            return null;
        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/goods/list-super-goods';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.3.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['type'] = 0;
        $params['pageId'] = 1;
        $params['pageSize'] = 5;
        $params['keyWords'] = $title;
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__, $e->getMessage());
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0 && $result_arr['data']['totalNum'] > 0){
            // 基本内容
            $first_baobei_item = $result_arr['data']['list'][0];

            if($first_baobei_item['goodsSign']){
                $goodsSign_arr = explode('-', $first_baobei_item['goodsSign']);

//                Cache::forget('tk:'.$goodsSign_arr[1]);
//                Cache::put('tk:'.$goodsSign_arr[1], $goodsSign_arr[0]);

                update_good_meta(-1, 'tk:'.$goodsSign_arr[1], $goodsSign_arr[0]);

                $other_good_item_id = 'tb__'.$goodsSign_arr[1];
            }else{
                $other_good_item_id = 'tb__'.$first_baobei_item['goodsId'];
            }

            $other_good = self::init_other_good_item($other_good_item_id, $first_baobei_item['shopType'] == 1 ? '天猫' : '淘宝',
                $first_baobei_item['mainPic'], $first_baobei_item['title'],
                $first_baobei_item['actualPrice'],$first_baobei_item);

            if($first_baobei_item['id'] > 0){
                $other_good['dtk_id'] = $first_baobei_item['id'];
            }

            if(isset($first_baobei_item['brandId']) && $first_baobei_item['brandId']){
                $other_good['brand']['brandId'] = _array_key($first_baobei_item, 'brandId', '');
                $other_good['brand']['brandName'] = _array_key($first_baobei_item, 'brandName', '');
            }
            if(isset($first_baobei_item['sellerId']) && $first_baobei_item['sellerId']){
                $other_good['seller']['sellerId'] = _array_key($first_baobei_item, 'sellerId', '');
                $other_good['seller']['shopName'] = _array_key($first_baobei_item, 'shopName', '');
                $other_good['seller']['shopLevel'] = _array_key($first_baobei_item, 'shopLevel', '');
                $other_good['seller']['shopLogo'] = _array_key($first_baobei_item, 'shopLogo', '');
            }

            if(isset($first_baobei_item['originalPrice']) && $first_baobei_item['originalPrice']){
                $other_good['origin_price'] = $first_baobei_item['originalPrice'];;
            }
            if(isset($first_baobei_item['imgs']) && $first_baobei_item['imgs']){
                $other_good['sliders'] = explode(',', $first_baobei_item['imgs']);
            }
            if(isset($first_baobei_item['smallImages']) && $first_baobei_item['smallImages'] && !is_array($first_baobei_item['smallImages'])){
                $first_baobei_item['smallImages'] = json_decode($first_baobei_item['smallImages'], true);
            }
            if(isset($first_baobei_item['smallImages']) && $first_baobei_item['smallImages'] && is_array($first_baobei_item['smallImages'])){
                if(_empty_($other_good['sliders'])){
                    $other_good['sliders'] = $first_baobei_item['smallImages'];
                }else{
                    foreach ($first_baobei_item['smallImages'] as $img){
                        $other_good['sliders'][] = $img;
                    }
                }
            }
            $other_good['flag'] = $first_baobei_item['shopType'] == 1 ? '天猫' : '淘宝';
            if(isset($first_baobei_item['desc']) && $first_baobei_item['desc']){
                $other_good['subtitle'] = $first_baobei_item['desc'];
            }
            if(!$other_good['subtitle'] && isset($first_baobei_item['dtitle']) && $first_baobei_item['dtitle']){
                $other_good['subtitle'] = $first_baobei_item['dtitle'];;
            }
            if(isset($first_baobei_item['video']) && $first_baobei_item['video']){
                $other_good['video'] = $first_baobei_item['video'];
            }
            if(isset($first_baobei_item['monthSales']) && $first_baobei_item['monthSales']){
                $other_good['monthSales'] = $first_baobei_item['monthSales'];
            }
            if(isset($first_baobei_item['detailPics']) && $first_baobei_item['detailPics']){
                $detailPics_arr = json_decode($first_baobei_item['detailPics'], true);
                if($detailPics_arr && count($detailPics_arr) > 1){
                    foreach ($detailPics_arr as $detailPics){
                        $other_good['detailPics'][] = $detailPics['img'];
                    }
                }else{
                    preg_match_all('$"img": "(.*?)"$', $first_baobei_item['detailPics'], $match_imgs);
                    if($match_imgs[1]){
                        foreach ($match_imgs[1] as $img_src){
                            $other_good['detailPics'][] = $img_src;
                        }
                    }
                }
            }

            if(_empty_($other_good['detailPics'])){
                $other_good['detailPics'] = $other_good['sliders'];
            }
            return ['code'=>1, 'data'=>$other_good];
        }else{
            _logger_(__file__, __line__,  '*********************get_taobao_item_detail错误【title:'.$title.'】：'.$result_arr['msg'].'************************************');
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

    private static function get_jingdong_item_detail($goodsId, $is_dtk_id = false)
    {
        if(!$goodsId){
            return null;
        }
        $ask_key = 'skuIds';
        if($is_dtk_id){
            $ask_key = 'id';
        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/dels/jd/goods/get-details';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params[$ask_key] = $goodsId;
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }
        $result_arr = json_decode($result, true);

        if($result_arr['code'] == 0){
            // 基本内容
            $other_good = self::init_other_good_item('jd__'.$goodsId, '京东',
                $result_arr['data'][0]['picMain'], $result_arr['data'][0]['skuName'],
                $result_arr['data'][0]['actualPrice'],$result_arr['data'][0]);

            if(isset($result_arr['data'][0]['id']) && $result_arr['data'][0]['id'] > 0){
                $other_good['dtk_id'] = $result_arr['data'][0]['id'];
            }

            if(isset($result_arr['data'][0]['shopId']) && $result_arr['data'][0]['shopId']){
                $other_good['seller']['sellerId'] = _array_key($result_arr['data'][0], 'shopId', '');
                $other_good['seller']['shopName'] = _array_key($result_arr['data'][0], 'shopName', '');
                // 是否旗舰店(1:是,0:否)
                $other_good['seller']['shopLevel'] = _array_key($result_arr['data'][0], 'isFlagship', 0);
            }
            if(isset($result_arr['data'][0]['detailImages']) && $result_arr['data'][0]['detailImages']){
                $other_good['detailPics'] = $result_arr['data'][0]['detailImages'];
            }
            if(isset($result_arr['data'][0]['originPrice']) && $result_arr['data'][0]['originPrice']){
                $other_good['origin_price'] = $result_arr['data'][0]['originPrice'];
            }

            if(isset($result_arr['data'][0]['extensionContent']) && $result_arr['data'][0]['extensionContent']){
                $other_good['subtitle'] = explode(',', $result_arr['data'][0]['extensionContent']);
            }
            if(isset($result_arr['data'][0]['isOwner']) && $result_arr['data'][0]['isOwner']){
                $other_good['flag'] = $result_arr['data'][0]['isOwner'] ? '京东自营' : '';
            }
            if(!$other_good['flag'] && $result_arr['data'][0]['isFlagship']){
                $other_good['flag'] = $result_arr['data'][0]['isFlagship'] ? '京东旗舰店' : '';
            }
            if(isset($result_arr['data'][0]['cid1Name']) && $result_arr['data'][0]['cid1Name']){
                $other_good['parent_cat'] = $result_arr['data'][0]['cid1Name'];
            }
            if(isset($result_arr['data'][0]['cid2Name']) && $result_arr['data'][0]['cid2Name']){
                $other_good['child_cat'] = $result_arr['data'][0]['cid2Name'];
            }
            if(isset($result_arr['data'][0]['inOrderCount30Days']) && $result_arr['data'][0]['inOrderCount30Days']){
                $other_good['monthSales'] = $result_arr['data'][0]['inOrderCount30Days'];
            }

//            $other_good['sliders'] = [$result_arr['data'][0]['picMain']];

            if(isset($result_arr['data'][0]['smallImages']) && $result_arr['data'][0]['smallImages'] && !is_array($result_arr['data'][0]['smallImages'])){
                $result_arr['data'][0]['smallImages'] = json_decode($result_arr['data'][0]['smallImages'], true);
            }

            if(isset($result_arr['data'][0]['smallImages']) && $result_arr['data'][0]['smallImages'] && is_array($result_arr['data'][0]['smallImages'])){
                foreach ($result_arr['data'][0]['smallImages'] as $img){
                    $other_good['sliders'][] = $img;
                }
            }

            return ['code'=>1, 'data'=>$other_good];
        }else{
            _logger_(__file__, __line__,  '*********************get_jingdong_item_detail错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

    private static function get_douyin_item_detail($goodsId){
        if(!$goodsId){
            return null;
        }

        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapiv2.dataoke.com/tiktok/tiktok-materials-products-details';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['productIds'] = $goodsId;
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }
        $result_arr = json_decode($result, true);

        if($result_arr['code'] == 0){
            // 基本内容
            if(_empty_(_array_key($result_arr['data'], 'list', []))){
                return ['code'=>0, 'msg'=>'大淘客未返回解析结果'];
            }
            $other_good = self::init_other_good_item('dy__'.$goodsId, '抖音',
                $result_arr['data']['list'][0]['cover'], $result_arr['data']['list'][0]['title'],
                $result_arr['data']['list'][0]['price'],$result_arr['data']['list'][0]);

            if(isset($result_arr['data']['list'][0]['detailUrl']) && $result_arr['data']['list'][0]['detailUrl']){
                $other_good['url'] = $result_arr['data']['list'][0]['detailUrl'];
            }

            if(isset($result_arr['data']['list'][0]['id']) && $result_arr['data']['list'][0]['id'] > 0){
                $other_good['dtk_id'] = $result_arr['data']['list'][0]['id'];
            }

            if(isset($result_arr['data']['list'][0]['shopId']) && $result_arr['data']['list'][0]['shopId']){
                $other_good['seller']['sellerId'] = _array_key($result_arr['data']['list'][0], 'shopId', '');
                $other_good['seller']['shopName'] = _array_key($result_arr['data']['list'][0], 'shopName', '');
                // 是否旗舰店(1:是,0:否)
                $other_good['seller']['shopLevel'] = _array_key($result_arr['data']['list'][0], 'shopTotalScore', 0);
            }
            if(isset($result_arr['data']['list'][0]['imgs']) && $result_arr['data']['list'][0]['imgs']){
                $other_good['detailPics'] = $result_arr['data']['list'][0]['imgs'];
            }
            if(isset($result_arr['data']['list'][0]['price']) && $result_arr['data']['list'][0]['price']){
                $other_good['origin_price'] = $result_arr['data']['list'][0]['price'];
            }

            if(isset($result_arr['data']['list'][0]['orderNum']) && $result_arr['data']['list'][0]['orderNum']){
                $other_good['monthSales'] = $result_arr['data']['list'][0]['orderNum'];
            }

//            $other_good['sliders'] = [$result_arr['data'][0]['picMain']];

            if(isset($result_arr['data']['list'][0]['imgs']) && $result_arr['data']['list'][0]['imgs'] && !is_array($result_arr['data']['list'][0]['imgs'])){
                $result_arr['data']['list'][0]['imgs'] = json_decode($result_arr['data']['list'][0]['imgs'], true);
            }

            if(isset($result_arr['data']['list'][0]['imgs']) && $result_arr['data']['list'][0]['imgs'] && is_array($result_arr['data']['list'][0]['imgs'])){
                foreach ($result_arr['data'][0]['imgs'] as $img){
                    $other_good['sliders'][] = $img;
                }
            }

            return ['code'=>1, 'data'=>$other_good];
        }else{
            _logger_(__file__, __line__,  '*********************get_douyin_item_detail错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }


    private static function get_douyin_item_link($goodsId, $goods_detail_link){
        if(!$goods_detail_link){
            return null;
        }

        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapiv2.dataoke.com/tiktok/tiktok-materials-products-details';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['externalInfo'] = '0';
        $params['productUrl'] = $goods_detail_link;
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            // 基本内容
            // return ['id'=>$others_goods_id.'_link', 'link'=>$link, 'coupon_link'=>'', 'word'=>'',
            //                'longWord'=>'', 'shortLink'=>'', 'data'=>$data];
            $other_good_link = self::init_other_good_link('dy__'.$goodsId, !_empty_($result_arr['data']['qrCode']) ? $result_arr['data']['qrCode']['url'] : '', $result_arr['data']);

            if(isset($result_arr['data']['dyPassword']) && $result_arr['data']['dyPassword']){
                $other_good_link['word'] = $result_arr['data']['dyPassword'];
            }

            return ['code'=>1, 'data'=>$other_good_link];
        }else{
            _logger_(__file__, __line__,  '*********************get_douyin_item_link错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

    private static function get_pingduoduo_item_detail($goodsId_sign, $is_sign = false)
    {
        if(!$goodsId_sign){
            return null;
        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/dels/pdd/goods/detail';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v2.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        if($is_sign){
            $params['goodsSign'] = $goodsId_sign;
        }else{
            $params['goodsId'] = $goodsId_sign;
        }
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            // 基本内容
            $other_good = self::init_other_good_item('pdd__'.$result_arr['data']['goodsSign'], '拼多多',
                $result_arr['data']['goodsImageUrl'], $result_arr['data']['goodsName'],
                $result_arr['data']['minGroupPrice'],$result_arr['data']);

            if(isset($result_arr['data']['id']) && $result_arr['data']['id'] > 0){
                $other_good['dtk_id'] = $result_arr['data']['id'];
            }

            if(isset($result_arr['data']['mallId']) && $result_arr['data']['mallId']){
                $other_good['seller']['sellerId'] = _array_key($result_arr['data'], 'mallId', '');
                $other_good['seller']['shopName'] = _array_key($result_arr['data'], 'mallName', '');
            }

            if(isset($result_arr['data']['goodsSign']) && $result_arr['data']['goodsSign'] ){
                $other_good['pdd_goodsSign'] = $result_arr['data']['goodsSign'];
            }

            return ['code'=>1, 'data'=>$other_good];
        }else{
            _logger_(__file__, __line__,  '*********************get_pingduoduo_item_detail错误【id:'.$goodsId_sign.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }


    private static function get_taobao_item_link($goodsId, $is_dtk_id = false){
        if(!$goodsId){
            return null;
        }
        $ask_key = 'goodsId';
        if($is_dtk_id){
            $ask_key = 'id';
        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/tb-service/get-privilege-link';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.3.1';
        //其他请求参数 根据接口文档需求选填
        $params = array();

        if($is_dtk_id){
            $params[$ask_key] = $goodsId;
        }else{
            if(strpos($goodsId, '-') === false){
//            $pre = Cache::get('tk:'.$goodsId);
                $pre = get_good_meta(-1, 'tk:'.$goodsId);
                if($pre){
                    $params[$ask_key] = $pre . '-' . $goodsId;
                }else{
                    $params[$ask_key] = $goodsId;
                }
            }else{
                $params[$ask_key] = $goodsId;
                $goodsId = explode('-', $goodsId)[1];
            }
        }

        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            // 基本内容
            // return ['id'=>$others_goods_id.'_link', 'link'=>$link, 'coupon_link'=>'', 'word'=>'',
            //                'longWord'=>'', 'shortLink'=>'', 'data'=>$data];
            $other_good_link = self::init_other_good_link('tb__'.$goodsId, $result_arr['data']['itemUrl'], $result_arr['data']);
            if(isset($result_arr['data']['couponClickUrl']) && $result_arr['data']['couponClickUrl']){
                $other_good_link['coupon_link'] = $result_arr['data']['couponClickUrl'];
            }
            if(isset($result_arr['data']['tpwd']) && $result_arr['data']['tpwd']){
                $other_good_link['word'] = $result_arr['data']['tpwd'];
            }
            if(isset($result_arr['data']['longTpwd']) && $result_arr['data']['longTpwd']){
                $other_good_link['longWord'] = $result_arr['data']['longTpwd'];
            }
            if(isset($result_arr['data']['shortUrl']) && $result_arr['data']['shortUrl']){
                $other_good_link['shortLink'] = $result_arr['data']['shortUrl'];
            }
            return ['code'=>1, 'data'=>$other_good_link];
        }else{
            _logger_(__file__, __line__,  '*********************get_taobao_item_link错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

    private static function get_jingdong_item_link($goodsId, $is_dtk_id = false){
        if(!$goodsId){
            return null;
        }
//        $ask_key = 'goodsId';
//        if($is_dtk_id){
//            $ask_key = 'id';
//        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/dels/jd/kit/promotion-union-convert';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v1.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['unionId'] = self::get_jd_unionid();
        $params['materialId'] = 'https://item.jd.com/'.$goodsId.'.html';
        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            // 基本内容
            // return ['id'=>$others_goods_id.'_link', 'link'=>$link, 'coupon_link'=>'', 'word'=>'',
            //                'longWord'=>'', 'shortLink'=>'', 'data'=>$data];
            $other_good_link = self::init_other_good_link('jd__'.$goodsId, _empty_($result_arr['data']['longUrl']) ? $result_arr['data']['shortUrl'] : '', $result_arr['data']);
            return ['code'=>1, 'data'=>$other_good_link];
        }else{
            _logger_(__file__, __line__,  '*********************get_jingdong_item_link错误【id:'.$goodsId.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }


    private static function get_pingduoduo_item_link($goodsid_Sign){
        if(!$goodsid_Sign){
            return null;
        }
        $c = new CheckSign();
        //接口地址 必填
        $c->host = 'https://openapi.dataoke.com/api/dels/pdd/kit/goods-prom-generate';
        //appKey  必填
        $c->appKey = self::get_app_key();
        //appSecret  必填
        $c->appSecret = self::get_app_secret();
        //版本号  必填
        $c->version = 'v2.0.0';
        //其他请求参数 根据接口文档需求选填
        $params = array();
        $params['goodsSign'] = $goodsid_Sign;
        $params['pid'] = Settings::get('app_dataoke_pdd_main_pid', '');

        //$request = $c->request($parame,'POST'); //接口特别说明需要POST请求才使用
        try{
            $result = $c->request($params);
            $result_arr = json_decode($result, true);
        }catch (\Exception $e){
            _logger_(__file__, __line__,  $e->getMessage() );
            return ['code'=>0, 'msg'=>$e->getMessage()];
        }

        if($result_arr['code'] == 0){
            // 基本内容
            // return ['id'=>$others_goods_id.'_link', 'link'=>$link, 'coupon_link'=>'', 'word'=>'',
            //                'longWord'=>'', 'shortLink'=>'', 'data'=>$data];
            $other_good_link = self::init_other_good_link('pdd__'.$goodsid_Sign, $result_arr['data']['mobileUrl'], $result_arr['data']);
            return ['code'=>1, 'data'=>$other_good_link];
        }else{
            _logger_(__file__, __line__,  '*********************get_pingduoduo_item_link错误【id_sign:'.$goodsid_Sign.'】：'.$result_arr['msg'].'************************************' );
            return ['code'=>0, 'msg'=>$result_arr['msg']];
        }
    }

}
